package cn.zwx.member.svc.fallback;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.enums.ResponseCodeEnum;
import cn.zwx.member.svc.dto.AddressCreateReqDTO;
import cn.zwx.member.svc.dto.AddressQueryDTO;
import cn.zwx.member.svc.dto.AddressUpdateReqDTO;
import cn.zwx.member.svc.facade.AddressServiceApi;
import cn.zwx.member.svc.vo.AddressRespVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhangwenxue
 */
@Component
public class AddressApiFallback implements FallbackFactory<AddressServiceApi> {
    private static final Logger logger = LoggerFactory.getLogger(AddressApiFallback.class);
    @Override
    public AddressServiceApi create(Throwable cause) {
        return new AddressServiceApi() {
            @Override
            public ResponseEntity<Long> createAddress(AddressCreateReqDTO createReqVO) {
                logger.error("createAddress fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public ResponseEntity<Boolean> updateAddress(AddressUpdateReqDTO updateReqVO) {
                logger.error("updateAddress fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public ResponseEntity<Boolean> deleteAddress(Long id,Long userId) {
                logger.error("deleteAddress fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public ResponseEntity<AddressRespVO> getAddress(AddressQueryDTO dto) {
                logger.error("getAddress fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public ResponseEntity<AddressRespVO> getDefaultUserAddress(Long userId) {
                logger.error("getDefaultUserAddress fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public ResponseEntity<List<AddressRespVO>> getAddressList(Long userId) {
                logger.error("getAddressList fallback",cause);
                return ResponseEntity.fail(ResponseCodeEnum.RPC_ERROR.getCode(),cause.getMessage());
            }

            @Override
            public AddressApiFallback create(Throwable cause) {
                logger.error("调用Member-Service fallback",cause);
                return null;
            }

        };
    }
}
